package com.xmx.usercenter.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import com.xmx.usercenter.entity.AddressEntity;
import com.xmx.usercenter.service.AddressService;
import com.xmx.common.utils.PageUtils;
import com.xmx.common.utils.R;
import io.swagger.annotations.Api;


/**
 * 收货地址表
 *
 * @author fbb
 * @email boyinbing@126.com
 * @date 2021-08-11 01:37:15
 */
@Api(tags = "AddressController", description = "收货地址表服务")
@RestController
@RequestMapping("usercenter/address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("usercenter:address:list")
    @ApiOperation("获取所有收货地址表信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = addressService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("usercenter:address:info")
    @ApiOperation("获取指定id的收货地址表详情")
    public R info(@PathVariable("id") Integer id){
		AddressEntity address = addressService.getById(id);

        return R.ok().put("data", address);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加收货地址表信息")
    //@RequiresPermissions("usercenter:address:save")
    public R save(@RequestBody AddressEntity address){
        QueryWrapper<AddressEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AddressEntity::getId,address.getUserId());
        int count = addressService.count(queryWrapper);
        if(count <=0)
            address.setIsDefault(true);
        addressService.save(address);
        return R.ok().put("data",address.getId());
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("更新指定id收货地址表信息")
    //@RequiresPermissions("usercenter:address:update")
    public R update(@RequestBody AddressEntity address){
		addressService.updateById(address);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除指定id收货地址表信息")
    //@RequiresPermissions("usercenter:address:delete")
    public R delete(@RequestBody Integer[] ids){
		addressService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/findByAuid")
    public R findByAuid( Integer userId,Integer addressId){
        AddressEntity addressEntity = addressService.findlistByuid(userId, addressId);
        return R.ok().put("data",addressEntity);
    }
    @GetMapping("/findlistByDefault")
    public R findlistByDefault(Integer userId){
        AddressEntity addressEntity = addressService.findlistByDefault(userId);
        return R.ok().put("data",addressEntity);
    }

    @GetMapping("/findListByUid")
    public R findListByUid(@RequestParam("userId")Integer userId){
        QueryWrapper<AddressEntity> queryWrapper  = new QueryWrapper<>();
        queryWrapper.lambda().eq(AddressEntity::getUserId,userId);

        List<AddressEntity> page = addressService.list(queryWrapper);
        Map data = new HashMap();
        data.put("total",page.size());
         data.put("pages",1);
         data.put("limit",page.size());
         data.put("page",1);
         data.put("list",page);
        return R.ok().put("data",data);
    }

}
